package io.polly.ldz.webflux.controller;


import org.springframework.http.HttpEntity;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.result.view.Rendering;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebSession;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.time.Duration;
import java.util.Locale;
import java.util.TimeZone;

/**
 * @author ldz11
 * @description：
 * @date 2025/3/11 21:44
 */
@RestController
public class HomeWorkController {

    @GetMapping("/")
    public ResponseEntity<String> getEntity() {
        return ResponseEntity.status(403).header("a","b").body("Hello World");

    }

    @GetMapping("/hw")
    public String getHomeWork(@RequestParam(value="key",required = false,defaultValue = "you") String key) {
        return "Hello World ："+key;
    }
    @GetMapping("/mono")
    public Mono<String> getMono(ServerWebExchange exchange, WebSession session, Locale locale, TimeZone timeZone, HttpEntity<String> httpEntity) {
//        ServerHttpResponse response = exchange.getResponse();
//        httpEntity.getBody()
        return Mono.just("mono");
    }

    //ServiceSentEvent
    @GetMapping(value = "/sse",produces= MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> getSse() {
        return Flux.range(1,10)
                .map(x -> "sse"+x)
                .delayElements(Duration.ofSeconds(1));
    }

    @GetMapping(value = "/sse2",produces= MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<ServerSentEvent<String>> getSse2() {
        return Flux.range(1,10)
                .map(x ->
                    ServerSentEvent.builder("ha"+x)
                            .id("ha"+x)
                            .comment("comment"+x)
                            .event("event"+x)
                            .build()
                )
                .delayElements(Duration.ofSeconds(1));
    }

    //类标签需为controller
    @GetMapping("/baidu")
    public Rendering getRendering() {
        return Rendering.redirectTo("www.baidu.com").build();

    }

}
